package com.cedarhd.brzx.controller;

import com.cedarhd.brzx.common.JsonResult;
import com.cedarhd.brzx.common.enums.ResultCode;
import com.cedarhd.brzx.common.utils.PoolUtil;
import com.cedarhd.brzx.common.utils.ValidatorUtil;
import com.cedarhd.brzx.domain.service.FileService;
import com.cedarhd.brzx.domain.service.SoaMemberService;
import com.cedarhd.brzx.dto.request.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;


/**
 * @author Evan
 * @date 2018/4/27
 */
@RestController
@Api(value = "通联支付会员接口", description = "通联支付会员接口")
public class SoaMemberController {
    private static Logger log = LoggerFactory.getLogger(SoaMemberController.class);

    private static final String OK = "ok";
    private static final String ERROR = "error";

    @Autowired
    private SoaMemberService memberService;

    @Autowired
    private FileService fileService;

    @RequestMapping(value = Routes.SOA_MEMBER_CREATE_MEMBER, method = RequestMethod.POST)
    @ApiOperation(value = "创建会员", notes = "创建会员", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult createMember(@RequestBody CreateMemberDTO req){
        log.info("createMember params = {}",req);
        try {
            ExecutorService executor = PoolUtil.getPoolExecutor();
            Runnable task = ()-> fileService.writeCreateMember(req);
            executor.execute(task);
        }catch (Exception e){
            log.info("writeCreateMember error!",e);
        }

        ValidatorUtil.validate(req);
        JSONObject response = memberService.createMember(req);
        try {
            ExecutorService executor = PoolUtil.getPoolExecutor();
            Runnable task = ()-> {
                fileService.writeCreateMemberResponse(response == null ? "" : response.toString() );
                fileService.writeCreateMemberResponse(response == null ? "" : response.toString() );
            };

            fileService.writeCreateMemberResponse(response == null ? "" : response.toString() );
            executor.execute(task);
        }catch (Exception e){
            log.info("writeCreateMember error!",e);
        }

        ValidatorUtil.checkJsonResp(response);
        return JsonResult.getFromResponse(response.toString());
    }

    @RequestMapping(value = Routes.SOA_MEMBER_SEND_VERIFICATION_CODE, method = RequestMethod.POST)
    @ApiOperation(value = "发送短信验证码", notes = "发送短信验证码", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult sendVerificationCode(@RequestBody SendVerificationCodeDTO req){
        log.info("sendVerificationCode params ={}",req);

        ValidatorUtil.validate(req);
        JSONObject response = memberService.sendVerificationCode(req);
        ValidatorUtil.checkJsonResp(response);

        return JsonResult.getFromResponse(response.toString());
    }

    @RequestMapping(value = Routes.SOA_MEMBER_BIND_PHONE, method = RequestMethod.POST)
    @ApiOperation(value = "绑定手机", notes = "绑定手机", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult bindPhone(@RequestBody BindPhoneDTO req){
        log.info("bindPhone params ={}",req);

        ValidatorUtil.validate(req);
        JSONObject response = memberService.bindPhone(req);
        ValidatorUtil.checkJsonResp(response);

        return JsonResult.getFromResponse(response.toString());
    }

    @RequestMapping(value = Routes.SOA_MEMBER_CHANGE_BIND_PHONE, method = RequestMethod.POST)
    @ApiOperation(value = "修改绑定手机", notes = "修改绑定手机", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult changeBindPhone(@RequestBody ChangeBindPhoneDTO req){
        log.info("changeBindPhone params ={}",req);

        ValidatorUtil.validate(req);
        JSONObject response = memberService.changeBindPhone(req);
        ValidatorUtil.checkJsonResp(response);

        return JsonResult.getFromResponse(response.toString());
    }

    @RequestMapping(value = Routes.SOA_MEMBER_GET_MEMBER_INFO, method = RequestMethod.GET)
    @ApiOperation(value = "查询会员信息", notes = "查询会员信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult getMemberInfo(String bizUserId){
        log.info("getMemberInfo params ={}",bizUserId);
        if(StringUtils.isBlank(bizUserId)){
            return JsonResult.renderMsg(ResultCode.FAILED.getCode(),"商户用户标识不能为空",null);
        }

        JSONObject response = memberService.getMemberInfo(bizUserId);
        ValidatorUtil.checkJsonResp(response);

        return JsonResult.getFromResponse(response.toString());
    }




    @RequestMapping(value = Routes.SOA_MEMBER_REAL_AUTH, method = RequestMethod.POST)
    @ApiOperation(value = "个人实名认证", notes = "个人实名认证", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public JsonResult realNameAuthentication(@RequestBody RealAuthDTO req) {
        log.info("setRealName parmas = {}" , req);
        ValidatorUtil.validate(req);
        JSONObject response = memberService.realNameAuthentication(req);
        ValidatorUtil.checkJsonResp(response);
        return JsonResult.getFromResponse(response.toString());

    }


}
